<?php

namespace app\admin\controller;

use app\common\BaseHelper as Helper;
use app\common\Common;

use app\admin\model\Image;
use app\admin\model\Crop;
use app\admin\model\Croptype;

use think\Request;

class Varieties extends Base
{
    public function _initialize() {
        parent::_initialize();

        $cropId = $this->request->param('cropId');
        $defaultWhere = [];
        $defaultWhere['cropId'] = $cropId;
        $this->defaultWhere = $defaultWhere;
        $this->searchFields = [
            'name' => [
                'label'     => '名称',
                'field'     => 'name',
                'type'      => 'text',
                'disabled'  => false,
                'condition' => 'like'
            ],
        ];

        // 农作物
        $this->assign('cropId', $cropId);
        $crop = db('crop')->where('id', $cropId)->field('name, typeId')->find();
        $this->assign('cropName', $crop['name']);
        // 类目
        $croptype = Croptype::where('id', $crop['typeId'])->field('id, name')->find();
        $this->assign('croptypeId', $croptype['id']);
        $this->assign('croptypeName', $croptype['name']);

        // BCN
        $breadcrumbs = [
            '<li><i class="fa fa-angle-right"></i>&nbsp;<span>农事服务</span></li>',
            '<li><i class="fa fa-angle-right"></i>&nbsp;<a href="'.url('admin/croptype/index').'" title="农作物大全" target="_self">农作物大全</a></li>',
            '<li><i class="fa fa-angle-right"></i>&nbsp;<a href="'.url('admin/crop/index', ['typeId' => $crop['typeId']]).'" title="农作物" target="_self">农作物</a></li>',
            '<li><i class="fa fa-angle-right"></i>&nbsp;<span>品种</span></li>'
        ];
        $bcn = $this->view->fetch('public/bcn', ['breadcrumbs' => $breadcrumbs]);
        $this->assign('bcn', $bcn);
    }

    public function index(){
        // 农作物
        $cropList = Crop::order('sorting ASC')->select();
        $this->assign('cropList', $cropList);

        return parent::index();
    }

    public function add(Request $request) {

        return parent::add($request);
    }

    public function addPost(Request $request, $redirect = '') {
        $redirect = url('admin/varieties/index', ['cropId' => $request->param('cropId')]);

        return parent::addPost($request, $redirect);
    }

    public function edit(Request $request) {

        return parent::edit($request);
    }

    public function editPost(Request $request, $redirect = ''){
        $redirect = url('admin/varieties/index', ['cropId' => $request->param('cropId')]);

        return parent::editPost($request, $redirect);
    }

    public function delete() {
        return parent::delete();
    }

    /**
     * 将品种移至另一农作物
     */
    public function move() {
        $request = $this->request;
        if($request->isAjax()){
            $param = $request->param();
            $model = model($this->model);

            $ids = (Array)$param['ids'];
            foreach ($ids as $id) {
                $result = $model->where('id', $id)->update(['cropId' => $param['cropId']]);
            }

            if($result !== false) {
                return $this->success('移动成功！');
            } else {
                return $this->error('移动失败！');
            }
        }
    }

    /**
     * 检查类目名是否已存在
     */
    public function checkName(){
        if($this->request->isAjax()){
            $name = $this->request->param('name');
            $model = model($this->model);

            $result = $model->where(['name' => $name])->field('name')->find();

            if($result){
                return json(['status' => 1]);
            }else{
                return json(['status' => 0]);
            }
        }
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);

            if(isset($filter['name']) && $filter['name']){
                $where['name'] = ['like', '%'.$filter['name'].'%'];
            }
        }

        return $where;
    }

}
